package flc.ast.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.stark.more.MorePrefUtil;
import com.stark.more.about.DefAboutActivity;
import com.stark.usersys.lib.UserModule;
import com.stark.usersys.lib.user.bean.CsContact;
import com.stark.usersys.lib.user.bean.User;
import com.stark.usersysui.lib.login.LoginActivity;
import com.stark.usersysui.lib.vip.VipCenterActivity;

import flc.ast.R;
import flc.ast.activity.SettingActivity;
import flc.ast.databinding.FragmentMyBinding;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.base.BaseWebviewActivity;
import stark.common.basic.base.GeneralEvtDialog;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.event.usersys.UserSysEventProxy;
import stark.common.basic.retrofit.INewReqRetCallback;
import stark.common.basic.utils.StringUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MyFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MyFragment extends BaseNoModelFragment<FragmentMyBinding> {


    @Override
    protected int onCreate() {
        return R.layout.fragment_my;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.ivSetting.setOnClickListener(this);
        mDataBinding.llVip.setOnClickListener(this);
        mDataBinding.llUserCenter.setOnClickListener(this);
        mDataBinding.llPrivacy.setOnClickListener(this);
        mDataBinding.llAgreement.setOnClickListener(this);
        mDataBinding.llFeedback.setOnClickListener(this);
        mDataBinding.llAbout.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivSetting://设置
                startActivity(SettingActivity.class);
                break;
            case R.id.llVip://我的会员
                if (!UserModule.userManager().isLogin()) {
                    LoginActivity.startForRet(this, null);
                } else {
                    VipCenterActivity.start(mContext);
                }
                break;
            case R.id.llUserCenter://用户中心
                UserSysEventProxy.getInstance().goUserCenter(getActivity());
                break;
            case R.id.llAbout://关于我们
                startActivity(new Intent(getActivity(), DefAboutActivity.class));
                break;
            case R.id.llPrivacy://隐私政策
                BaseWebviewActivity.openAssetPrivacy(getActivity());
                break;
            case R.id.llFeedback://意见反馈
                BaseWebviewActivity.openFeedback(getActivity());
                break;
            case R.id.llAgreement://用户协议
                BaseWebviewActivity.openAssetTerms(mContext);
                break;
        }
    }

    @Override
    protected void initData() {
        boolean showPersonalRecommend = MorePrefUtil.showPersonalRecommend();
        if (showPersonalRecommend) {
            //显示个性化开关控件
            mDataBinding.ivSetting.setVisibility(View.VISIBLE);
        } else {
            //隐藏个性化开关控件
            mDataBinding.ivSetting.setVisibility(View.GONE);
        }

        mDataBinding.llUserCenter.setVisibility(UserSysEventProxy.getInstance().supportUserSys() ? View.VISIBLE : View.GONE);

        UserModule.userManager().getUserLiveData().observe(this, new Observer<User>() {
            @Override
            public void onChanged(User user) {
                if (user == null) {
                    //未登录
                    mDataBinding.ivVipImg.setImageResource(R.drawable.wchy);
                    mDataBinding.tvVipText.setText(getString(R.string.not_open_vip));
                    mDataBinding.tvVipDuration.setVisibility(View.GONE);
                } else {
                    //已登录
                    UserModule.userApi().getUserInfo(MyFragment.this, new INewReqRetCallback<User>() {
                        @Override
                        public void onResult(int code, String msg, @Nullable User result) {
                            if (result.isVip()) {
                                mDataBinding.ivVipImg.setImageResource(R.drawable.ychy);
                                mDataBinding.tvVipText.setText(getString(R.string.my_vip));
                                mDataBinding.tvVipDuration.setVisibility(View.VISIBLE);
                                mDataBinding.tvVipDuration.setText(getString(R.string.vip_duration) + result.getVipInfo().getExpire_time());
                            } else {
                                mDataBinding.ivVipImg.setImageResource(R.drawable.wchy);
                                mDataBinding.tvVipText.setText(getString(R.string.not_open_vip));
                                mDataBinding.tvVipDuration.setVisibility(View.GONE);
                            }
                        }
                    });
                }
            }
        });

    }
}